/*
 *  YATUN  - Yet another tunneling application
 *
 *  Michal Demin - 2007
 *
 *  main.cpp
 *  Hlavny program
 *
 */
 

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <signal.h>

#include <sys/select.h>
#include <sys/time.h>
#include <sys/types.h>

#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>

#include <errno.h>

#include <vector>

#include "server.h"
#include "client.h"

#define MAX(x,y) ((x)>(y)?(x):(y))


int quit = 0;

static void
sig_term (int sig)
{
  fprintf (stderr, "Interrupt recieved  !!! \n");
  quit = 1;
}

int main (int argc, char **argv) {
    int ret;
    int len;
    int state;
    int server = 0;
    int reliable = 0;
    int dyntimeout = 0;
    int connected = 0;
    int interface_up = 0;
    int inPort = DEFAULT_PORT;

    struct sockaddr_in *remoteaddr;

    struct sigaction sa;

    char *ScriptUP = new char[100];
    char *ScriptDOWN = new char[100];


/* variable pre select */
    struct timeval sel_timeout;
    fd_set rfds;
    int maxfd;

    struct timeval Timeout,Timestamp;
  
/* variable pre getopt */
    int opt;
    int daemon = 0;
    int daemon_cfg = 0;
    char *ConfigFile = NULL;

    memset (Pack, 0, sizeof (Packet));

/* parsneme command line */

    while ((opt = getopt (argc, argv, "dc:")) != -1) {
	switch (opt) {
	    case 'd':
		daemon = 1;
		break;
	    case 'c':
		len = strlen (optarg);
		ConfigFile = new char[len + 1];

		memset (ConfigFile, 0, len + 1);
		memcpy (ConfigFile, optarg, len);
		break;
	    default:			/* '?' */
		fprintf (stderr, "Usage: %s [-d] [-c config]\n", argv[1]);
		exit (EXIT_FAILURE);
	}
    }

    if (ConfigFile == NULL) {
	len = strlen (DEFAULT_CONFIG);
	ConfigFile = new char[len + 1];

	memset (ConfigFile, 0, len + 1);
	memcpy (ConfigFile, DEFAULT_CONFIG, len);
    }
  
    if (Config->Open (ConfigFile) == -1) {
	fprintf (stderr, "Cannot open config file \n");
	exit (-1);
    }

    if (Config->GetSection ("main")) {
	fprintf (stderr, "Section [Main] does not exist !!! \n");
        exit (-1);
    }

    if (Config->GetPropertyByType ("server", &server, T_BOOL)) {
	printf("No mode specified ... select srver or client ... \n");
	exit(-1);
    }

    Config->GetPropertyByType ("daemon", &daemon_cfg, T_BOOL);
    daemon |= daemon_cfg;

    if(server) {
	YatunServerConf ServerConf;
	YatunServer *Server = new YatunServer(Config);
	
	ServerConf.UseMServer = 0;
	Config->GetPropertyByType ("UseMServer", &ServerConf.UseMServer, T_BOOL);
	if(ServerConf.UseMServer) {
	    ServerConf.ServerName = GetPropertyString("ServerName");
	    ServerConf.mServerAddress = GetPropertyString("mServerAddress");
	    Config->GetPropertyByType("mServerPort", &ServerConf.mServerPort, T_INT);
	}
	
	Config->GetPropertyByType("Port", &ServerConf.Port, T_INT);
	// execute server .... 	
	Server->SetConf(ServerConf);
	Server->Run();
    } else {
    // els klient sme
	YatunClientConf ClientConf;
	YatunClient *Client = new YatunClient(Config);
		
	ClientConf.SessionName = GetPropertyString("SessionName");
	ClientConf.UseMServer = 0;
	Config->GetPropertyByType("UseMServer", &ClientConf.UseMServer, T_BOOL);
	if(ClientConf.UseMServer) {
	    mServerAddress = GetPropertyString("mServerAddress");
	    Config->GetPropertyByType("mServerPort", &ClientConf.mServerPort, T_INT);
	}
	ClientConf.Port = DEFAULT_PORT;
	Config->GetPropertyByType("Port", &ClientConf.Port, T_INT);
	ClientConf = GetPropertyString("ServerAddress");
	// executes the start of the client ... .... ... ... ... ...
	Client->SetConf(ClientConf);
    }



    delete[]ConfigFile;
    delete Config;
    
    return 0;
}
